package com.swehr.servlet;

import com.swehr.model.Position;
import com.swehr.model.ResultInfo;
import com.swehr.proxy.PersonnelServiceProxy;
import org.codehaus.jackson.map.ObjectMapper;
import org.codehaus.jackson.type.TypeReference;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.List;

/**
 * @author Rock Huang
 * @version 0.1
 */
public class PositionServlet extends HttpServlet {
    protected void doGet(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletUtils.printRequestInfo(req);
        if (ServletUtils.toSignin(req, resp)) return;
        req.setAttribute("checked", "Position");
        ServletUtils.forward(req, resp);
    }

    protected void doPost(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletUtils.printRequestInfo(req);
        ObjectMapper mapper = new ObjectMapper();
        String json = req.getParameter("carrier");
        List<Position> pos = mapper.readValue(json, new TypeReference<List<Position>>(){});

        ResultInfo rs = new PersonnelServiceProxy().savePos(pos);
        resp.getWriter().println(rs.toString());
    }

    protected void doPut(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletUtils.printRequestInfo(req);
    }

    protected void doDelete(HttpServletRequest req, HttpServletResponse resp)
            throws ServletException, IOException {
        ServletUtils.printRequestInfo(req);
    }
}
